package com.example.myapplication.screen

import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch


@Composable
fun PagerDemo(innerPadding: PaddingValues) {
    //定义一个滑动页的状态变量
    val pagerState = rememberPagerState(pageCount = { 5 })

    //获取上下文对象
    val context = LocalContext.current
    val scope = rememberCoroutineScope()

    // 使用 LaunchedEffect 监听当前页的变化
    LaunchedEffect(pagerState) {
        //观察pagerState.currentPage的变化，如果变化则执行collect中的代码。
        snapshotFlow { pagerState.currentPage }.collect { page ->
            // 在这里响应页面变化，例如打印当前页
            println("当前页为：" + page)
            //Toast.makeText(context,"这是一个提示 ${page}", Toast.LENGTH_SHORT).show()
        }
    }

    Box(
        contentAlignment = Alignment.BottomCenter   //靠下对齐
    ) {
        HorizontalPager(
            state = pagerState,
            beyondViewportPageCount = 1,
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) { page ->
            Text(
                text = "Page Item $page",
                textAlign = TextAlign.Center,
                fontSize = 50.sp,
                modifier = Modifier
                    .fillMaxSize()
                    .background(
                        color = when (page % 3) {
                            1 -> Color.Red
                            2 -> Color.Blue
                            else -> Color.Cyan
                        }
                    )
                    .wrapContentHeight(Alignment.CenterVertically)
            )
        }

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(
                    bottom = 50.dp
                ),
            horizontalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterHorizontally)
        ) {

            repeat(pagerState.pageCount) { index ->
                Text(
                    text = "${pagerState.currentPage}/${pagerState.pageCount}",
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .size(50.dp)
                        .clickable{
                            scope.launch {
                                pagerState.animateScrollToPage(index,
                                    animationSpec = tween(
                                        durationMillis = 600,
                                        easing = LinearOutSlowInEasing
                                    )
                                )
                            }
                        }
                        .background(
                            color = if (pagerState.currentPage == index) Color.Magenta else Color.LightGray,
                            shape = RoundedCornerShape(10.dp)
                        )
                        .wrapContentHeight(Alignment.CenterVertically)
                )
            }
        }
    }

}